// pages/user-profile/user-profile.js
const app = getApp()

Page({
  data: {
    userInfo: null,
    loading: true,
    editing: false,
    formData: {
      nickname: '',
      phone: '',
      email: '',
      avatar: ''
    }
  },

  onLoad() {
    this.loadUserInfo()
  },

  onShow() {
    this.loadUserInfo()
  },

  // 加载用户信息
  loadUserInfo() {
    this.setData({
      loading: true
    })

    app.userRequest({
      url: '/getUserInfo'
    }).then((res) => {
      const userInfo = res.data || {}
      this.setData({
        userInfo: userInfo,
        formData: {
          nickname: userInfo.nickname || '',
          phone: userInfo.phone || '',
          email: userInfo.email || '',
          avatar: userInfo.avatar || ''
        },
        loading: false
      })
    }).catch((err) => {
      console.error('获取用户信息失败:', err)
      this.setData({
        loading: false
      })
      wx.showToast({
        title: '获取用户信息失败',
        icon: 'none'
      })
    })
  },

  // 开始编辑
  startEdit() {
    this.setData({
      editing: true
    })
  },

  // 取消编辑
  cancelEdit() {
    this.setData({
      editing: false,
      formData: {
        nickname: this.data.userInfo.nickname || '',
        phone: this.data.userInfo.phone || '',
        email: this.data.userInfo.email || '',
        avatar: this.data.userInfo.avatar || ''
      }
    })
  },

  // 保存编辑
  saveEdit() {
    const { nickname, phone, email } = this.data.formData

    if (!nickname.trim()) {
      wx.showToast({
        title: '请输入昵称',
        icon: 'none'
      })
      return
    }

    wx.showLoading({
      title: '保存中...'
    })

    app.userRequest({
      url: '/updateUserInfo',
      method: 'POST',
      data: {
        nickname: nickname.trim(),
        phone: phone.trim(),
        email: email.trim()
      }
    }).then((res) => {
      wx.hideLoading()
      wx.showToast({
        title: '保存成功',
        icon: 'success'
      })
      
      // 更新本地数据
      this.setData({
        userInfo: {
          ...this.data.userInfo,
          nickname: nickname.trim(),
          phone: phone.trim(),
          email: email.trim()
        },
        editing: false
      })
    }).catch((err) => {
      wx.hideLoading()
      console.error('保存用户信息失败:', err)
      wx.showToast({
        title: '保存失败',
        icon: 'none'
      })
    })
  },

  // 输入昵称
  onNicknameInput(e) {
    this.setData({
      'formData.nickname': e.detail.value
    })
  },

  // 输入手机号
  onPhoneInput(e) {
    this.setData({
      'formData.phone': e.detail.value
    })
  },

  // 输入邮箱
  onEmailInput(e) {
    this.setData({
      'formData.email': e.detail.value
    })
  },

  // 选择头像
  chooseAvatar() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.uploadAvatar(res.tempFilePaths[0])
      }
    })
  },

  // 上传头像
  uploadAvatar(filePath) {
    wx.showLoading({
      title: '上传中...'
    })

    app.userRequest({
      url: '/uploadAvatar',
      method: 'POST',
      data: {
        avatar: filePath
      }
    }).then((res) => {
      wx.hideLoading()
      if (res.data && res.data.avatar_url) {
        this.setData({
          'formData.avatar': res.data.avatar_url,
          'userInfo.avatar': res.data.avatar_url
        })
        wx.showToast({
          title: '头像上传成功',
          icon: 'success'
        })
      }
    }).catch((err) => {
      wx.hideLoading()
      console.error('上传头像失败:', err)
      wx.showToast({
        title: '头像上传失败',
        icon: 'none'
      })
    })
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          app.userLogout()
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }
      }
    })
  },

  // 跳转到编辑页面
  goToEdit() {
    wx.navigateTo({
      url: '/pages/user-edit/user-edit?mode=edit'
    })
  },

  // 跳转到报工记录页面
  goToRecords() {
    wx.navigateTo({
      url: '/pages/user-records/user-records'
    })
  },

  // 跳转到工资统计页面
  goToWage() {
    wx.navigateTo({
      url: '/pages/user-wage/user-wage'
    })
  }
}) 